#include <algorithm>
#include <cstdio>
#define lowbit(x) (x & (-x))

typedef long long ll;
const int N = 100005;
int n, k, val[N];
ll ans;

struct Node {
  int x, y, z;
  inline bool operator<(const Node &rhs) {
    return x == rhs.x ? (y == rhs.y ? z < rhs.z : y > rhs.y) : x < rhs.x;
  }
} a[N], b[N];

inline void modify(int i, int v) {
  while (i <= n) val[i] += v, i += lowbit(i);
}

inline ll query(int i) {
  i = std::min(i, n);
  if (i <= 0) return 0;
  ll res = 0;
  while (i) res += val[i], i -= lowbit(i);
  return res;
}

inline void merge(int l, int r, int mid) {
  int cnt = l - 1, i = l, j = mid + 1;
  while (i <= mid && j <= r)
    if (a[i].y > a[j].y)
      b[++cnt] = a[i++];
    else
      b[++cnt] = a[j++];
  while (i <= mid) b[++cnt] = a[i++];
  while (j <= r) b[++cnt] = a[j++];
  for (i = l; i <= r; ++i) a[i] = b[i];
}

void divs(int l, int r) {
  if (l == r) return;
  int mid = (l + r) >> 1, i = l, j = mid + 1;
  divs(l, mid), divs(mid + 1, r);
  while (j <= r) {
    while (i <= mid && a[i].y > a[j].y) modify(a[i++].z, 1);
    ans += query(a[j].z - k - 1) - query(a[j].z + k) + query(n);
    ++j;
  }
  for (j = l; j < i; ++j) modify(a[j].z, -1);
  merge(l, r, mid);
}

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("testdata.in", "r", stdin);
  freopen("testdata.out", "w", stdout);
#endif
#ifndef LOCAL
  freopen("CppTest.in", "r", stdin);
  freopen("CppTest.out", "w", stdout);
#endif
#endif

  scanf("%d%d", &n, &k);
  for (int i = 1, tmp; i <= n; ++i) {
    scanf("%d", &tmp);
    a[tmp].x = i;
  }
  for (int i = 1, tmp; i <= n; ++i) {
    scanf("%d", &tmp);
    a[tmp].y = i;
    a[tmp].z = tmp;
  }
  std::sort(a + 1, a + n + 1);
  divs(1, n);
  printf("%lld", ans);
  return 0;
}